﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace PRE.Database
{
    using System.Configuration;
    using System.IO;

    public partial class FrmSetup : DevExpress.XtraEditors.XtraForm
    {
        private const string STR_FILE = "xQKS.sql";
        private const string STR_NOT_FILE = "Không tìm thấy file CSDL!";
        private const string STR_SUCCESS = "Thành công!";
        private const string STR_ERROR = "Lỗi: {0}";

        public FrmSetup()
        {
            InitializeComponent();
        }

        private void cmdOk_Click(object sender, EventArgs e)
        {
            string sver = cbbServer.Text;
            string user = cbbUser.Text;
            string pass = txtPass.Text;
            string data = cbbDb.Text;

            // LINQ to SQL class
            string strLS;

            if (cbbAuthen.EditValue == "Windows")
                strLS = String.Format(@"Data Source={0};Initial Catalog={1};Integrated Security=True", sver, data);
            else
                strLS = String.Format(@"Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3}", sver, data, user, pass);

            Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            _config.ConnectionStrings.ConnectionStrings[1].ConnectionString = strLS;
            _config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection(_config.ConnectionStrings.SectionInformation.Name);
            Properties.Settings.Default.Reload();

            Close();
        }

        private void ckSavePass_CheckedChanged(object sender, EventArgs e)
        {
            Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            string strCnn = _config.ConnectionStrings.ConnectionStrings[1].ConnectionString;
            var x = strCnn.Split('=');
         //   txtPass.Text = x.Last();
        }

        private void cbbAuthen_EditValueChanged(object sender, EventArgs e)
        {
            if (cbbAuthen.EditValue == "Windows")
            {
                cbbUser.Properties.ReadOnly = true;
                txtPass.Properties.ReadOnly = true;
            }
            else
            {
                cbbUser.Properties.ReadOnly = false;
                txtPass.Properties.ReadOnly = false;
            }
        }

        private void cmdSetup_Click(object sender, EventArgs e)
        {
            //var stmp = Text;

            //try
            //{
            //    cbbDb.Text = DAL.BaseDAL.DefautDbName;
            //    Text = "Xin cho trong giay lat...";

            //    string createQuery;
            //   // var db = new DAL.BaseDAL();

            //    if (File.Exists(STR_FILE) == true)
            //    {
            //        StreamReader objreader;
            //        string tmp; createQuery = "";
            //        objreader = new StreamReader(STR_FILE);
            //        int i = -1;
            //        while (!objreader.EndOfStream) // khi chưa đọc hết file
            //        {
            //            tmp = objreader.ReadLine();

            //            // Loại bỏ Go và -- trong file SQL
            //            if (tmp != "Go")
            //            {
            //                if (!tmp.StartsWith("--")) // không tìm thấy
            //                {
            //                    i = tmp.LastIndexOf("--");
            //                    if (i == -1)
            //                        createQuery += tmp + Environment.NewLine;
            //                    else
            //                        createQuery += tmp.Substring(0, i) + Environment.NewLine;
            //                }
            //            }
            //            else
            //            {
            //               // DAL.BaseDAL.ExecuteCommand(createQuery); 
            //                createQuery = "";
            //            }
            //        }

            //        objreader.Close();
            //        MessageBox.Show(STR_SUCCESS, Text);
            //        Close();
            //    }
            //    else MessageBox.Show(STR_NOT_FILE, Text);
            //}
            //catch (Exception)
            //{
            //    try
            //    {
            //        DAL.BaseDAL.CreateDbName();
            //        cmdSetup_Click(sender, e);
            //    }
            //    catch (Exception ex)
            //    {
            //        MessageBox.Show(String.Format(STR_ERROR, ex.Message), Text);
            //    }
            //}
            //finally
            //{
            //    Text = stmp;
            //}
        }

        private void cmdClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }




    }
}